<?php 
    $mtime = microtime(); 
    $mtime = explode(" ",$mtime); 
    $mtime = $mtime[1] + $mtime[0]; 
    $starttime = $mtime; 
?>
<?php
include_once "test2.php";  //fungsi2...

$ta=$_POST["ta"];
$sem=$_POST["sem"];
include_once "query1.php";
include_once "query2.php";
include_once "test5.php";  //query2, insert2...
$popsize=$_POST["popsize"];	//banyaknya kemungkinan
$gen=$_POST["gen"];		//banyaknya generasi
$matkul=$hitung1;		//banyaknya mata kuliah
$jadwal=30;		//banyaknya slot jadwal
$pm=($_POST["pm"]/100);		//persen mutasi 8%
$gens=1;
include_once "test12.php";
/*
$ta="2009/2010";
$sem="1";
include_once "query1.php";
include_once "test5.php";  //query2, insert2...
$popsize="50";	//banyaknya kemungkinan
$gen="1000";		//banyaknya generasi
$matkul=$hitung1;		//banyaknya mata kuliah
$jadwal=30;		//banyaknya slot jadwal
$pm="0.25";		//persen mutasi 25%
$gens=1;
*/

/*
	$pop_best=round($popsize/2);				//50% popsize
	$pop_rand=round(($popsize-$pop_best)/2);	//25% popsize
	$pop_mut=$popsize-($pop_best+$pop_rand);	//50% induk = 25% popsize
*/
$pop_rand=round($popsize/2);	//50% popsize
$pop_best=round(($popsize-$pop_rand)/2);				//25% popsize
$pop_mut=$popsize-($pop_best+$pop_rand);	//100% induk = 25% popsize

echo "<strong>Parameter:<br/>Tahun Ajaran: ".$ta."<br/>Semester: ".$sem."<br/>Populasi: ".$popsize."<br/>Generasi: ".$gen."<br/>Jumlah Mata Kuliah: ".$matkul."<br/>Slot Jadwal: ".$jadwal."<br/>Permutasi: ".($pm*100)."%<br/>Mata Kuliah: ".$hitung1."</strong><br /><br />";
for (;$gens<=$gen;$gens++){
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if ($gens<2){
echo "Generasi ke-";echo $gens."<br/>";
/*===========================================================================
Pembangkitan Generasi + Fitness.....
//===========================================================================*/
/*echo "<table border='1' style='text-align:center;'>";
echo "<tr><td></td>";
for ($a=1;$a<=$matkul;$a++)				//++
{echo "<td>".$a."</td>";}
echo "<td>fitness</td></tr>";*/		//++
for ($a=1;$a<=$popsize;$a++)
{
//echo "<tr><td>Individu-".$a."</td>";				//
	for ($b=1;$b<=$matkul;$b++)
	{
		//echo "<td>";									//
		$ggg[$b]=$kromosom[$a][$b]=mt_rand(1,$jadwal);
		//echo $ggg[$b]."</td>";							//
	}
$fit_sum[$a]=quickCount($ggg);
$fit_av=$fit_av + $fit_sum[$a];
//echo "<td>".$fit_sum[$a]."</td>";			//
//echo "</tr>";								//
}

$fit_min=max($fit_sum);
$fit_where=inWhere($fit_sum, $fit_min);
$fit_av2=round(($fit_av/$popsize), 6);
//echo "<tr><td>Best</td><td colspan='4'>".$fit_min."</td><td colspan='3'>Where</td><td>".$fit_where."</td></tr>";
//echo "<tr><td>Average</td><td colspan='4'>".$fit_av2."</td></tr>";
//echo "</table><br/>";
unset($fit_gbest);unset($fit_gbwhere);unset($fit_gaverage);unset($ggg);unset($fit_av);
}


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
else {
echo "Generasi ke-";echo $gens."<br/>";
/*===========================================================================
Generasi 1++
//===========================================================================*/
/*echo "<table border='1' style='text-align:center;'>";
echo "<tr><td></td>";
for ($a=1;$a<=$matkul;$a++)				//++
{echo "<td>".$a."</td>";}
echo "<td>fitness</td></tr>";*/			//++
for ($a=1;$a<=$popsize;$a++)
{
//echo "<tr><td>Individu-".$a."</td>";				//
	for ($b=1;$b<=$matkul;$b++)
	{
		//echo "<td>";									//
		$kromosom[$a][$b]=$krom_new2[$a][$b];
		$ggg[$b]=$kromosom[$a][$b];
		//echo $ggg[$b]."</td>";							//
	}
$fit_sum[$a]=quickCount($ggg);
$fit_av=$fit_av + $fit_sum[$a];
//echo "<td>".$fit_sum[$a]."</td>";			//
//echo "</tr>";								//
}

$fit_min=max($fit_sum);
$fit_where=inWhere($fit_sum, $fit_min);
$fit_av2=round(($fit_av/$popsize), 6);
//echo "<tr><td>Best</td><td colspan='4'>".$fit_min."</td><td colspan='3'>Where</td><td>".$fit_where."</td></tr>";
//echo "<tr><td>Average</td><td colspan='4'>".$fit_av2."</td></tr>";
//echo "</table><br/>";
unset($ggg);unset($fit_av);
}


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*===========================================================================
Proses Seleksi "Good Fitness".....
===========================================================================*/
$sorting=sorting($fit_sum);$i=0;
foreach($sorting as $key => $val){
$i++;$urutan[$i]=$val;
//echo $urutan[$i]."; ";
}
unset($key);unset($val);

/*echo "<table border='1' style='text-align:center;'>";
echo "<tr><td></td>";
for ($a=1;$a<=$matkul;$a++)				//++
{echo "<td>".$a."</td>";}
echo "<td>fitness</td></tr>";*/		//++
for ($a=1;$a<=$popsize;$a++)
{
$urut=$urutan[$a];
//echo "<tr><td>Individu-".$a."</td>";				//
	for ($b=1;$b<=$matkul;$b++)
	{
		//echo "<td>";										//
		$krom_sel[$a][$b]=$kromosom[$urut][$b];
		$ggg[$b]=$krom_sel[$a][$b];
		//echo $ggg[$b]."</td>";							//
	}
$fit_sum[$a]=quickCount($ggg);
$fit_av=$fit_av + $fit_sum[$a];
//echo "<td>".$fit_sum[$a]."</td>";			//
//echo "</tr>";								//
}

$fit_min=max($fit_sum);
$fit_where=inWhere($fit_sum, $fit_min);
$fit_av2=round(($fit_av/$popsize), 6);
//echo "<tr><td>Best</td><td colspan='4'>".$fit_min."</td></tr>";
//echo "<tr><td>Average</td><td colspan='4'>".$fit_av2."</td></tr>";
//echo "<tr><td>".$fit_min."</td>"."<td>".$fit_where."</td><td>".$fit_av2."</td></tr>";
//echo "</table><br/>";

/*===========================================================================
1/2 populasi terbaik = induk
===========================================================================*/
/*echo $pop_best.";".$pop_rand.";".$pop_mut;				//++
echo "<table border='1' style='text-align:center;'>";
echo "<tr><td></td>";
for ($a=1;$a<=$matkul;$a++)
{echo "<td>".$a."</td>";}
echo "<td>fitness</td></tr>";*/		//++
for ($a=1;$a<=$pop_best;$a++)
{
//echo "<tr><td>Individu-".$a."</td>";				//
	for ($b=1;$b<=$matkul;$b++)
	{
		//echo "<td>";									//
		$krom_pop_best[$a][$b]=$krom_sel[$a][$b];
		$ggg[$b]=$krom_pop_best[$a][$b];
		//echo $ggg[$b]."</td>";							//
	}
$fit_sum_best[$a]=quickCount($ggg);
$fit_av_best=$fit_av_best + $fit_sum_best[$a];
//echo "<td>".$fit_sum_best[$a]."</td>";			//
//echo "</tr>";								//
}
//echo "</table><br/>";								//
/*===========================================================================
1/4 populasi random
===========================================================================*/
/*echo "<table border='1' style='text-align:center;'>";				//++
echo "<tr><td></td>";
for ($a=1;$a<=$matkul;$a++)
{echo "<td>".$a."</td>";}
echo "<td>fitness</td></tr>";*/		//++
for ($a=1;$a<=$pop_rand;$a++)
{
//echo "<tr><td>Individu-".$a."</td>";				//
	for ($b=1;$b<=$matkul;$b++)
	{
		//echo "<td>";									//
		$krom_pop_rand[$a][$b]=mt_rand(1,$jadwal);
		$ggg[$b]=$krom_pop_rand[$a][$b];
		//echo $ggg[$b]."</td>";							//
	}
$fit_sum_rand[$a]=quickCount($ggg);
$fit_av_rand=$fit_av_rand + $fit_sum_rand[$a];
//echo "<td>".$fit_sum_rand[$a]."</td>";			//
//echo "</tr>";								//
}
//echo "</table><br/>";								//
/*===========================================================================
1/4 populasi mutasi Induk.....
===========================================================================*/
/*echo "<table border='1' style='text-align:center;'>";
echo "<tr><td></td>";
for ($a=1;$a<=$matkul;$a++)				//++
{echo "<td>".$a."</td>";}
echo "<td>fitness</td></tr>";*/		//++
for ($a=1;$a<=$pop_mut;$a++)
{
//=======================================================================
$jumlah_mut=round($pm*$matkul);$i=1;

for($aa=1;$aa<=$matkul;$aa++){
$acak=(mt_rand(0,999))/1000;
//echo $acak."-";
	if ($acak<$pm && $i<=$jumlah_mut){
	//echo "mutasi";
	$letak[$i]=$aa;$i++;
	}
}unset($i);
//$ubah_mut1=$kromosom[$a][($letak[1])];$ubah_mut2=$kromosom[$a][($letak[2])];
$ubah_mut1=$krom_pop_best[$a][$letak[1]];$new1=mutasiin($ubah_mut1);
$ubah_mut2=$krom_pop_best[$a][$letak[2]];$new2=mutasiin($ubah_mut2);
//=======================================================================
//echo "<tr><td>Individu-$a</td>";				//
	for ($b=1;$b<=$matkul;$b++)
	{
		//echo "<td>";									//
		if($b==$letak[1]){
				$ggg[$b]=$new1;}
		elseif($b==$letak[2]){
				$ggg[$b]=$new2;}
		else{			
				$ggg[$b]=$krom_pop_best[$a][$b];}
		$krom_pop_mut[$a][$b]=$ggg[$b];
		//echo $krom_pop_mut[$a][$b];					//
	}
//echo "<td>$letak[1] : $letak[2]</td>";
unset($letak);unset($ubah_mut1);unset($ubah_mut2);
$fit_sum_mut[$a]=quickCount($ggg);
$fit_av_mut=$fit_av_mut + $fit_sum_mut[$a];
//if ($ubah_mut1>1){
//	echo "<td>".$fit_sum_mut[$a]."*</td>";
//	}
//	else{
//	echo "<td>".$fit_sum_mut[$a]."</td>";
//	}
//echo "<td>".$fit_sum_mut[$a]."</td>";			//
//echo "</tr>";						//
}
//echo "</tr></table>";				//
							

/*===========================================================================
New Populasi, Mixed.....
===========================================================================*/
//echo "<table border='1' style='text-align:center;'>";			// [ 1/2 populasi terbaik = induk..... ]
//echo "<tr><td></td>";
unset($fit_av_new);/*
for ($a=1;$a<=$matkul;$a++)				//++
{echo "<td>".$a."</td>";}
echo "<td>fitness</td></tr>";*/				//++
for ($a=1;$a<=$pop_best;$a++)
{$reserved=$a;
//echo "<tr><td>Individu-".$reserved."</td>";				//
	for ($b=1;$b<=$matkul;$b++)
	{
		//echo "<td>";									//
		$krom_new[$reserved][$b]=$krom_pop_best[$a][$b];
		$ggg[$b]=$krom_new[$reserved][$b];
		//echo $ggg[$b]."</td>";							//
	}
$fit_sum_new[$reserved]=quickCount($ggg);
$fit_av_new=$fit_av_new + $fit_sum_new[$reserved];
//echo "<td>".$fit_sum_new[$reserved]."</td>";			//
//echo "</tr>";								//
}

for ($a=1;$a<=$pop_rand;$a++)									// [ 1/4 populasi random..... ]
{$reserved=$a+$pop_best;
//echo "<tr><td>Individu-".$reserved."</td>";				//
	for ($b=1;$b<=$matkul;$b++)
	{
		//echo "<td>";									//
		$krom_new[$reserved][$b]=$krom_pop_rand[$a][$b];
		$ggg[$b]=$krom_new[$reserved][$b];
		//echo $ggg[$b]."</td>";							//
	}
$fit_sum_new[$reserved]=quickCount($ggg);
$fit_av_new=$fit_av_new + $fit_sum_new[$reserved];
//echo "<td>".$fit_sum_new[$reserved]."</td>";			//
//echo "</tr>";								//
}

for ($a=1;$a<=$pop_mut;$a++)									// [ 1/4 populasi mutasi Induk..... ]
{$reserved=$a+$pop_best+$pop_rand;
//echo "<tr><td>Individu-".$reserved."</td>";				//
	for ($b=1;$b<=$matkul;$b++)
	{
		//echo "<td>";									//
		$krom_new[$reserved][$b]=$krom_pop_mut[$a][$b];
		$ggg[$b]=$krom_new[$reserved][$b];
		//echo $ggg[$b]."</td>";							//
	}
$fit_sum_new[$reserved]=quickCount($ggg);
$fit_av_new=$fit_av_new + $fit_sum_new[$reserved];
//echo "<td>".$fit_sum_new[$reserved]."</td>";			//
//echo "</tr>";								//
}
$fit_gbest[$gens]=$fit_min_new=max($fit_sum_new);
$fit_gbwhere[$gens]=$fit_where_new=inWhere($fit_sum_new, $fit_min_new);
$fit_gaverage[$gens]=$fit_av2_new=round(($fit_av_new/$popsize), 6);
//echo "<tr><td>Best</td><td colspan='4'>".$fit_gbest[$gens]."</td><td colspan='3'>Where</td><td>".$fit_gbwhere[$gens]."</td></tr>";  //
//echo "<tr><td>Average</td><td colspan='4'>".$fit_gaverage[$gens]."</td></tr>";       //
//echo "</table><br/>";               //
//echo $fit_gbest[$gens]."<br/>";
unset($kromosom);unset($fit_sum);unset($fit_av);unset($fit_min);
unset($fit_where);unset($fit_av2);unset($ggg);


/*===========================================================================
Proses Seleksi "Good Fitness 2".....
===========================================================================*/
$sorting2=sorting($fit_sum_new);$i=0;
foreach($sorting2 as $key => $val){
$i++;
$urutan[$i]=$val;
}
unset($key);unset($val);unset($urut);

echo "<table border='1' style='text-align:center;'>";
echo "<tr><td></td>";/*
for ($a=1;$a<=$matkul;$a++)				//++
{echo "<td>".$a."</td>";}
echo "<td>fitness</td></tr>";*/		//++
for ($a=1;$a<=$popsize;$a++)
{
$urut=$urutan[$a];
//echo "<tr><td>Individu-".$a."</td>";				//
	for ($b=1;$b<=$matkul;$b++)
	{
		//echo "<td>";										//
		$krom_new2[$a][$b]=$krom_new[$urut][$b];
		$ggg[$b]=$krom_new2[$a][$b];
		//echo $ggg[$b]."</td>";							//
	}
$fit_sum_new2[$a]=quickCount($ggg);
$fit_av_new2=$fit_av_new2 + $fit_sum_new2[$a];
//echo "<td>".$fit_sum_new2[$a]."</td>";			//
//echo "</tr>";								//
}

$fit_gbest2[$gens]=$fit_min_new2=max($fit_sum_new2);
$fit_gbwhere2[$gens]=$fit_where_new2=inWhere($fit_sum_new2, $fit_min_new2);
$fit_gaverage2[$gens]=$fit_av2_new2=round(($fit_av_new2/$popsize), 6);
echo "<tr><td>Best</td><td colspan='4'>".$fit_gbest2[$gens]."</td><td colspan='3'>Where</td><td>".$fit_gbwhere2[$gens]."</td></tr>";
echo "<tr><td>Average</td><td colspan='4'>".$fit_gaverage2[$gens]."</td></tr>";
echo "</table><br/>"; 
unset($kromosom);unset($fit_sum_new2);unset($fit_av_new2);unset($fit_min_new2);
unset($fit_where_new2);unset($fit_av2_new2);
//usleep(110000);
}		//kurawal for


echo "\n \n \n";
for ($probe=1;$probe<=$matkul;$probe++)
	{
	$rakuen2[$probe]= $krom_new2[1][$probe];
	//echo $rakuen[$probe]."<br/>";
	}
$rakuen=dicek($rakuen2, $jadwal, $matkul, $htggg2);

    $mtime = microtime(); 
    $mtime = explode(" ",$mtime); 
    $mtime = $mtime[1] + $mtime[0]; 
    $endtime = $mtime; 
    $totaltime = ($endtime - $starttime); 
	$ekse=round($totaltime, 4);


?>

<form name='lempar' method='POST' action='hasil.php'>
<input type='hidden' name='ta' id='ta' value="<?php echo $ta ?>">
<input type='hidden' name='sem' id='sem' value="<?php echo $sem ?>">
<input type='hidden' name='ekse' id='ekse' value="<?php echo $ekse ?>">
<input type='hidden' name='matkul' id='matkul' value="<?php echo $matkul ?>">
<input type='hidden' name='fitgen' id='fitgen' value="<?php echo $fit_gbest2[$gen] ?>">
<input type='hidden' name='fitav' id='fitav' value="<?php echo $fit_gaverage2[$gen] ?>">
<?php
for($urut_f=1;$urut_f<=$jadwal;$urut_f++){
$urutp=1;
$urutpp=0;
	for($urut_ff=1;$urut_ff<=$matkul;$urut_ff++){
		if($rakuen[$urut_ff]==$urut_f){
		$urutpp++;
		//echo $urutp;
		echo "<input type='hidden' name='probe".$urut_f,$urutpp."' id='probe".$urut_f,$urutpp."' value='";
		echo $urutp;
		echo "'>\n";
		}
	$urutp++;
	}
$j_urut[$urut_f]=$urutpp;
}
echo "<br/>";
for($urut_f=1;$urut_f<=$jadwal;$urut_f++){
echo "<input type='hidden' name='jprobe".$urut_f."' id='jprobe".$urut_f."' value='";
echo $j_urut[$urut_f];
echo "'>\n";
}
?>
<input type='submit' value='Simpan'>
</form>

<?php
$rapiin=1;
for (;$rapiin<=$gen;$rapiin++){
echo $fit_gbest2[$rapiin];echo "<br />";
}
?>
<?php/*
$ver= 9;
$rev= 13;
$author= "Mahadiputra S";
$path= "http://code.google.com/p/mahadiputra-ta"
change:
rev 14.
-optimize
*/
?>